// pages/index/zone/goodsDetail/appraise/publish/cachieventlist/cachieventlist.js
import { axios } from '../../../../../../utils/ajax.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:'',
    page:1,
    totlepage:'',
    userid: '',
    token: '',
    canload: false,
    goodsid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    this.setData({
      userid: userid,
      token: token,
      goodsid: options.goodsid
    })
    this.getdata()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page
    if (page < this.data.totlepage){
      page++ 
      this.setData({
        page: page
      })
      this.getdata(true)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //选择商品
  choosethis(e) {
    let index = e.currentTarget.dataset.index
    console.log(index)
    let data = this.data.data
    data.map(x => x.checked = false)
    data[index].checked = true
    this.setData({
      data: data
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  // 获取上一页实例   可以获取上一页的数据并改变
    prevPage.setData({
      data: data[index]
    })
    wx.navigateBack({
      delta:1
    })
  },
  //获取数据
  getdata (loadmore = false) {
    axios({
      url: '/bask/baskgoodslist',
      data: {
        userid: this.data.userid,
        token: this.data.token,
        index: this.data.page
      }
    }).then((res) => {
      console.log(res)
      if (loadmore){
        this.setData({
          data: this.data.data.concat(res.data.goodsInfo) 
        })
      }else{
        this.setData({
          data: res.data.goodsInfo
        })  
      }
      //找到选中项
      let data = this.data.data
      data.find(x => x.goodsId == this.data.goodsid)?data.find(x => x.goodsId == this.data.goodsid).checked = true:''
      this.setData({
        data: data
      })
      
      let totlepage = Math.ceil(res.data.totals / 10)
      this.setData({
        totlepage: totlepage
      })
      if (this.data.page < this.data.totlepage){
        this.setData({
          canload: true
        })
      }else{
        this.setData({
          canload: false
        })      
      }
    })
  }
})